package org.springboot.szsrcb.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.Req_base_info;
import org.springboot.szsrcb.entity.Req_bill;
import org.springboot.szsrcb.entity.Req_team_info_log;
import org.springboot.szsrcb.entity.Req_workload_info;
import org.springboot.szsrcb.entity.Test_base_info;
import org.springboot.szsrcb.entity.Test_workload_info;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.Req_bill_Service;
import org.springboot.szsrcb.srvice.Test_bill_Service;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/test")
public class TestRestController_workload {

	private static final Logger logger = LoggerFactory.getLogger(TestRestController_workload.class);

	@Autowired
	private Test_bill_Service test_bill_Service;

	@Autowired
	private UserService userService;

	@RequestMapping(value = "/test_modify_detail_workload_info_onload", method = { RequestMethod.GET })
	public String req_modify_detail_workload_info_onload(HttpServletRequest request) {
		String test_id = request.getParameter("test_id");
		// List<Req_bill> r = req_bill_Service.req_search(test_id);
		if (!test_id.equals("")) {
			String s_tmp = trans_2_json_modify_req_workload_info(test_id);
			logger.info("test_assign_detail_onload:" + s_tmp);
			return s_tmp;
		} else {
			return null;
		}

	}

	private String trans_2_json_modify_req_workload_info(String test_id) {
		Test_base_info test_base_info= test_bill_Service.search_a_test_base_info_by_testid(test_id);
		float test_plan_workload=test_base_info.getTest_workload();
		
		//model.addAttribute("test_plan_workload",test_plan_workload);
		
		String r_start = "[" + "{type: 'block',  list:[" + "{type:'block',list:["
					+ "{type : 'hidden',name : 'test_id',label : 'Test ID',labelWidth:'70',position:'label-left', value:'"+ test_id + "'},"
					+ "{type : 'hidden',name : 'test_plan_workload',label : 'test_plan_workload',labelWidth:'70',position:'label-left', value:'"+ String.valueOf(test_plan_workload) + "'}"
				+ "]}," 
				+ "{type:'block',list:["
				+ "{type : 'button',name : 'base_info_save_btn',value : '保存修改信息'}," + "]}," + "{type:'block',list:["
				+ "{type : 'label',labelWidth:'250',name : 'count_1',label : '计划工作量合计  ：          人天  '},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',labelWidth:'250',name : 'count_2',label : '实际工作量合计  ：             人天'},"
				// +"{type : 'newcolumn'},"
				+ "]}," + "{type:'block',list:["
				+ "{type : 'label',name : 'count_3',labelWidth:'250',label : '测试案例数（联机）  ：          人天  '},"
				+ "{type : 'newcolumn'},"
				+ "{type : 'label',name : 'count_4',labelWidth:'250',label : '测试案例数（批处理）  ：             人天'}" + "]}"
				+ "]" + "}" + "]";
		String r_s = r_start;
		logger.info("retrun info :" + r_start);
		return r_s;
	}

	@RequestMapping(value = "/test_modify_detail_workload_info_grid", method = { RequestMethod.GET })
	public String req_modify_detail_workload_info_grid(HttpServletRequest request) {
		logger.info("access /test/test_modify_detail_workload_info_grid");
		String test_id = request.getParameter("test_id");
		logger.info("test_id =" + test_id);
		// Req_base_info r = req_bill_Service.search_req_base_info(test_id);
		Test_base_info r = test_bill_Service.search_a_test_base_info_by_testid(test_id);
		// r.setReq_id(Integer.parseInt(g_id));
		// if(r!=null){
		String s_tmp = trans_2_json_test_modify_detail_workload_info_grid(test_id);
		logger.info("test_modify_detail_team_info_grid :" + s_tmp);
		return s_tmp;
		/*
		 * }else{ return null; }
		 */
	}

	private String trans_2_json_test_modify_detail_workload_info_grid(String test_id) {

		String r_s = "";
		// String test_id=r.getReq_id();
		String r_start = "{rows:[";
		String r_end = "]}";
		String r_content = "";
		String req_head_value = "";
		int count_id = 1;
		// Req_bill req=req_bill_Service.req__bill_search_by_id(r.getReq_id());
		Test_base_info test_base_info = test_bill_Service.search_a_test_base_info_by_testid(test_id);
		// 获取req_head
		String test_head = test_base_info.getTest_head();
		Map<String, Object> map_head = new HashMap<String, Object>();
		map_head.put("count_value", count_id);
		map_head.put("input_value", test_head);
		map_head.put("role", "测试负责人");
		map_head.put("test_id", test_id);
		// map_head.put("old_info_list",old_info_list);
		Map<String, Object> map_head_result = trans_json_value(map_head);
		String req_head_content = (String) map_head_result.get("json_value");
		count_id = (int) map_head_result.get("count_value");

		/*
		 * //获取req_analyst String req_analyst=test_base_info.getReq_analyst();
		 * Map<String, Object> map_analyst = new HashMap<String, Object>();
		 * map_analyst.put("count_value", count_id);
		 * map_analyst.put("input_value", req_analyst); map_analyst.put("role",
		 * "需求分析人"); map_analyst.put("req_id", test_id); Map<String, Object>
		 * map_analyst_result=trans_json_value(map_analyst);
		 * 
		 * String req_analyst_content=(String)
		 * map_analyst_result.get("json_value"); count_id=(int)
		 * map_analyst_result.get("count_value");
		 */
		// 获取req_supervisor
		String test_executor = test_base_info.getTest_executor();
		Map<String, Object> map_supervisor = new HashMap<String, Object>();
		map_supervisor.put("count_value", count_id);
		map_supervisor.put("input_value", test_executor);
		map_supervisor.put("role", "测试执行人");
		map_supervisor.put("test_id", test_id);
		Map<String, Object> map_supervisor_result = trans_json_value(map_supervisor);

		String req_supervisor_content = (String) map_supervisor_result.get("json_value");
		count_id = (int) map_supervisor_result.get("count_value");

		// 非当前项目成员 工作量显示
		// 1.获取非当前项目成员obj
		List<Test_workload_info> obj = test_bill_Service.search_test_workload_info(test_id);
		if (obj != null) {
			List<Test_workload_info> no_team_member = new ArrayList<Test_workload_info>();
			for (int i = 0; i < obj.size(); i++) {
				Test_workload_info element = obj.get(i);
				String g_name = element.getUser_name();
				// String tmp=req_head+req_analyst+req_supervisor;
				String g_e_role = element.getRole();
				String role1 = "测试负责人";
				String role2 = "测试执行人";
				// String role3="需求监理人";
				if (g_e_role.equals(role1)) {
					if (!test_head.contains(g_name)) {
						no_team_member.add(element);
					}
				}
				if (g_e_role.equals(role2)) {
					if (!test_executor.contains(g_name)) {
						no_team_member.add(element);
					}
				}
				/*
				 * if(g_e_role.equals(role3)){
				 * if(!req_supervisor.contains(g_name)){
				 * no_team_member.add(element); } }
				 */
			}
			String no_team_member_content = "";
			if (no_team_member.size() > 0) {
				for (int i = 0; i < no_team_member.size(); i++) {
					Test_workload_info array_element = no_team_member.get(i);
					Map<String, Object> map_no_team = new HashMap<String, Object>();
					map_no_team.put("count_value", count_id);
					map_no_team.put("input_value", array_element.getUser_name());
					map_no_team.put("role",array_element.getRole());
					map_no_team.put("test_id", test_id);
					Map<String, Object> r_map_no_team = trans_json_value2(map_no_team);
					no_team_member_content = no_team_member_content + "," + (String) r_map_no_team.get("json_value");
					count_id = (int) r_map_no_team.get("count_value");
				}
			}

			r_s = r_start + req_head_content + "," + req_supervisor_content + no_team_member_content + r_end;
		} else {
			r_s = r_start + req_head_content + "," + req_supervisor_content + r_end;
		}
		// }
		return r_s;
	}
	
	@SuppressWarnings("unchecked")
	private Map<String, Object> trans_json_value2(Map<String, Object> map) {
		String return_v = "";
		String tmp_content = "";

		int count_value = (int) map.get("count_value");
		String test_id = (String) map.get("test_id");
		String user_list = (String) map.get("input_value");
		// List<Test_workload_info>
		// old_info_list=(List<Test_workload_info>)map.get("old_info_list");
		// Req_base_info req_base_info=(Req_base_info)map.get("req_base_info");
		String role = (String) map.get("role");
		// userlist
		if (user_list == null || user_list.equals("")) {
			tmp_content = "";
		} else {
			String[] s_array = user_list.split(",");
			for (int i = 0; i < s_array.length; i++) {
				String tmp_start = "{ id:" + (count_value) + ",data:[";
				String tmp_end = "]}";
				if ((i + 1) < s_array.length) {
					tmp_end = tmp_end + ",";
				}
				// ID
				tmp_content = "'" + count_value + "',";
				// String tmp_content="";
				// name
				tmp_content = tmp_content + "'" + s_array[i] + "',";
				// 角色
				tmp_content = tmp_content + "'" + role + "',";
				// 计划完成日
				Test_workload_info obj = test_bill_Service.search_a_test_workload_info(test_id, s_array[i], role);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				if (obj == null) {
					// 计划完成日
					tmp_content = tmp_content + "'',";
					// 实际完成日
					tmp_content = tmp_content + "'',";
					// 计划工作量
					tmp_content = tmp_content + "'',";
					// 实际工作量
					tmp_content = tmp_content + "''";
				} else {

					// 计划完成日
					String date_tmp = "";
					if (obj.getPlan_complete_date() != null) {
						date_tmp = sdf.format(obj.getPlan_complete_date());
					}
					tmp_content = tmp_content + "'" + date_tmp + "',";
					// 实际完成日
					if (obj.getActual_complete_date() != null) {
						date_tmp = sdf.format(obj.getActual_complete_date());
					}

					tmp_content = tmp_content + "'" + date_tmp + "',";
					// 计划工作量
					// if (obj.getPlan_workload()==null) {
					// tmp_content=tmp_content+"'0',";
					// } else {
					tmp_content = tmp_content + "'" + obj.getPlan_workload() + "',";
					// }

					// 实际工作量
					// if (obj.getActual_workload()==null) {
					// tmp_content=tmp_content+"'0'";
					// } else {
					tmp_content = tmp_content + "'" + obj.getActual_workload() + "',";
					// }

					// 测试案例数 联机
					tmp_content = tmp_content + "'" + obj.getOnline_testcase_count() + "',";
					// 测试案例数 批处理
					tmp_content = tmp_content + "'" + obj.getBatch_testcase_count() + "'";
				}
				tmp_content = tmp_start + tmp_content + tmp_end;
				return_v = return_v + tmp_content;
				count_value = count_value + 1;
			}
		}
		// return_v=tmp_content;
		Map<String, Object> map_r = new HashMap<String, Object>();
		map_r.put("json_value", return_v);
		map_r.put("count_value", count_value);
		return map_r;

	}

	@SuppressWarnings("unchecked")
	private Map<String, Object> trans_json_value(Map<String, Object> map) {
		String return_v = "";
		String tmp_content = "";

		int count_value = (int) map.get("count_value");
		String test_id = (String) map.get("test_id");
		String user_list = (String) map.get("input_value");
		// List<Test_workload_info>
		// old_info_list=(List<Test_workload_info>)map.get("old_info_list");
		// Req_base_info req_base_info=(Req_base_info)map.get("req_base_info");
		String role = (String) map.get("role");
		// userlist
		// 取旧值用户list
		/*List<String> old_user_list = test_bill_Service.search_test_workload_info_by_role(test_id, role);
		// 判断旧值是否在userlist中，不在增加
		if (old_user_list != null) {
			String[] tmp = user_list.split(",");
			for (int i = 0; i < old_user_list.size(); i++) {
				String u = old_user_list.get(i);
				boolean is_add = true;
				for (int j = 0; j < tmp.length; j++) {
					if (tmp[j].equals(u)) {
						is_add = false;
					}
				}
				if (is_add) {
					user_list = user_list + "," + u;
				}

			}
		}*/

		if (user_list == null || user_list.equals("")) {
			tmp_content = "";
		} else {
			String[] s_array = user_list.split(",");
			for (int i = 0; i < s_array.length; i++) {
				String tmp_start = "{ id:" + (count_value) + ",data:[";
				String tmp_end = "]}";
				if ((i + 1) < s_array.length) {
					tmp_end = tmp_end + ",";
				}
				// ID
				tmp_content = "'" + count_value + "',";
				// String tmp_content="";
				// name
				tmp_content = tmp_content + "'" + s_array[i] + "',";
				// 角色
				tmp_content = tmp_content + "'" + role + "',";
				// 计划完成日
				Test_workload_info obj = test_bill_Service.search_a_test_workload_info(test_id, s_array[i], role);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				if (obj == null) {
					// 计划完成日
					tmp_content = tmp_content + "'',";
					// 实际完成日
					tmp_content = tmp_content + "'',";
					// 计划工作量
					tmp_content = tmp_content + "'',";
					// 实际工作量
					tmp_content = tmp_content + "''";
				} else {

					// 计划完成日
					String date_tmp = "";
					if (obj.getPlan_complete_date() != null) {
						date_tmp = sdf.format(obj.getPlan_complete_date());
					}
					tmp_content = tmp_content + "'" + date_tmp + "',";
					// 实际完成日
					if (obj.getActual_complete_date() != null) {
						date_tmp = sdf.format(obj.getActual_complete_date());
					}

					tmp_content = tmp_content + "'" + date_tmp + "',";
					// 计划工作量
					// if (obj.getPlan_workload()==null) {
					// tmp_content=tmp_content+"'0',";
					// } else {
					tmp_content = tmp_content + "'" + obj.getPlan_workload() + "',";
					// }

					// 实际工作量
					// if (obj.getActual_workload()==null) {
					// tmp_content=tmp_content+"'0'";
					// } else {
					tmp_content = tmp_content + "'" + obj.getActual_workload() + "',";
					// }

					// 测试案例数 联机
					tmp_content = tmp_content + "'" + obj.getOnline_testcase_count() + "',";
					// 测试案例数 批处理
					tmp_content = tmp_content + "'" + obj.getBatch_testcase_count() + "'";
				}
				tmp_content = tmp_start + tmp_content + tmp_end;
				return_v = return_v + tmp_content;
				count_value = count_value + 1;
			}
		}
		// return_v=tmp_content;
		Map<String, Object> map_r = new HashMap<String, Object>();
		map_r.put("json_value", return_v);
		map_r.put("count_value", count_value);
		return map_r;

	}

	@RequestMapping(value = "/test_modify_detail_workload_info_save")
	public String test_modify_detail_workload_info_save(HttpServletRequest request, Model model) {
		String test_id = request.getParameter("test_id");
		// List<Req_bill> r = req_bill_Service.req_search(g_id);

		// String
		// s_tmp="req_bill_no:"+r.get(0).getReq_bill_no()+";req_title:"+r.get(0).getReq_title()+";req_type:"+r.get(0).getReq_type()+";req_dept:"+r.get(0).getReq_offer_dept()+";req_complete_date:"+r.get(0).getRequired_completion_date();
		// String s_tmp=trans_2_json_modify_req_workl_info(r.get(0));

		int g_count_rows = Integer.parseInt(request.getParameter("count_rows"));
		int g_count_columns = Integer.parseInt(request.getParameter("count_columns"));
		String save_info = request.getParameter("save_info");
		String[] s_tmp = save_info.split(";", g_count_rows);

		for (int i = 0; i < s_tmp.length; i++) {
			String[] str = s_tmp[i].split(",", g_count_columns);
			// 判断当前用户,更新
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_name", str[1]);
			map.put("role", str[2]);
			map.put("plan_complete_date", str[3]);
			map.put("actual_complete_date", str[4]);
			map.put("plan_workload", str[5]);
			map.put("actual_workload", str[6]);
			map.put("online_testcase_count", str[7]);
			map.put("batch_testcase_count", str[8]);
			map.put("test_id", Integer.parseInt(test_id));
			boolean is_save=true;
			String v="";
			if(str[3].equals(v)&&str[4].equals(v)&&str[5].equals(v)&&str[6].equals(v)&&str[7].equals(v)&&str[8].equals(v)){
				is_save=false;
			}
			if (is_save) {
			boolean r = test_bill_Service.test_workload_info_save(map);
			}

		}

		logger.info("access /req/req_modify_detail_workload_info_save");

		model.addAttribute("test_id", test_id);
		// String tmp="/req/req_modify_detail_workload_info_save?req_id="+g_id;
		// model.addAttribute("action", tmp);
		// Test_base_info r =
		// test_bill_Service.search_a_test_base_info_by_testid(test_id);
		String r_tmp = trans_2_json_test_modify_detail_workload_info_grid(test_id);
		return r_tmp;

	}
}
